package org.edu.modules.maker.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.fegin.UserFeginClient;
import org.edu.fegin.domain.VeUser;
import org.edu.modules.maker.entity.VeCuzCourseMaker;
import org.edu.modules.maker.service.IVeCuzCourseMakerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"团队制作人"})
@RestController
@RequestMapping({"/course/veCuzCourseMaker"})
public class VeCuzCourseMakerController
        extends BaseController<VeCuzCourseMaker, IVeCuzCourseMakerService>
{
    private static final Logger log = LoggerFactory.getLogger(VeCuzCourseMakerController.class);
    @Autowired
    private IVeCuzCourseMakerService veCuzCourseMakerService;
    @Autowired
    private UserFeginClient userFeginClient;

    @AutoLog("团队制作人-分页查询")
    @ApiOperation(value="分页查询", notes="团队制作人-分页查询")
    @PostMapping({"/list"})
    public Result<?> queryPageList(VeCuzCourseMaker veCuzCourseMaker, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCourseMaker> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCourseMaker, req.getParameterMap());
        Page<VeCuzCourseMaker> page = new Page(pageNo.intValue(), pageSize.intValue());
        IPage<VeCuzCourseMaker> pageList = this.veCuzCourseMakerService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("团队制作人-通过课程id查询")
    @ApiOperation(value="通过课程id查询", notes="团队制作人-通过课程id查询")
    @PostMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="courseId", required=true) long courseId)
    {
        List<VeCuzCourseMaker> veCuzCourseMaker = this.veCuzCourseMakerService.list((Wrapper)new QueryWrapper().eq("courseId", Long.valueOf(courseId)));
        if (veCuzCourseMaker == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veCuzCourseMaker);
    }

    @AutoLog("团队制作人-新增信息")
    @ApiOperation(value="新增信息", notes="团队制作人-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeCuzCourseMaker veCuzCourseMaker)
    {
        this.veCuzCourseMakerService.save(veCuzCourseMaker);
        return Result.OK("添加成功！");
    }

    @ApiOperation(value="增加制作团队人信息", notes="团队制作人-新增信息")
    @PostMapping({"/addTeam"})
    public Result<?> addTeam(@RequestParam(name="id", required=true) String ids, @RequestParam(name="courseId", required=true) Integer courseId)
    {
        List<VeCuzCourseMaker> makerList = new ArrayList();
        Object result = this.userFeginClient.queryCourseUser(ids).getResult();

        List<VeUser> list = JSON.parseArray(JSON.toJSONString(result), VeUser.class);
        for (VeUser veUser : list)
        {
            VeCuzCourseMaker veCuzCourseMaker = new VeCuzCourseMaker();
            veCuzCourseMaker.setCourseid(courseId);
            veCuzCourseMaker.setMakername(veUser.getNickname());
            veCuzCourseMaker.setMakerid(veUser.getId());
            veCuzCourseMaker.setTerminalid(veUser.getTerminalid());
            makerList.add(veCuzCourseMaker);
        }
        this.veCuzCourseMakerService.saveBatch(makerList);
        return Result.OK("添加成功！");
    }

    @AutoLog("团队制作人-修改信息")
    @ApiOperation(value="修改信息", notes="团队制作人-修改信息")
    @PostMapping({"/edit"})
    public Result<?> edit(@RequestBody VeCuzCourseMaker veCuzCourseMaker)
    {
        this.veCuzCourseMakerService.updateById(veCuzCourseMaker);
        return Result.OK("编辑成功!");
    }

    @AutoLog("团队制作人-通过id删除")
    @ApiOperation(value="通过id删除", notes="团队制作人-通过id删除")
    @PostMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veCuzCourseMakerService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("团队制作人-批量删除")
    @ApiOperation(value="批量删除", notes="团队制作人-批量删除")
    @PostMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veCuzCourseMakerService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @AutoLog("团队制作人-我创建的课程分页查询")
    @ApiOperation(value="我创建的课程分页查询", notes="团队制作人-我创建的课程分页查询")
    @PostMapping({"/MyCreateList"})
    public Result<?> MyCreateList(VeCuzCourseMaker veCuzCourseMaker, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        QueryWrapper<VeCuzCourseMaker> queryWrapper = QueryGenerator.initQueryWrapper(veCuzCourseMaker, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        int makerId = 0;
        if (sysUser != null) {
//            makerId = sysUser.getUserid().intValue();
        }
        veCuzCourseMaker.setMakerid(Integer.valueOf(makerId));
        queryWrapper.setEntity(veCuzCourseMaker);
        IPage<Map<String, Object>> pageList = this.veCuzCourseMakerService.pageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }
}
